#include "DirectXCapture.h"
#define D3DX

#include <windows.h>
#include <QtGui>
#include <qt_windows.h>
#ifdef D3DX
#include <d3d9.h>
#endif
#include "DirectXCapture.h"

DirectXCapture::DirectXCapture(QWidget*wnd)
{
#ifdef D3DX
		m_pd3dDevice = NULL;
		m_pD3D = NULL;
		m_pSurface = NULL;
		m_SelSurface = NULL;

		parent = wnd;
		if(!InitDirectX()){
				qDebug("Init DirectX failed");
		}
		else{
				D3DDISPLAYMODE mode;
				m_pd3dDevice->GetDisplayMode(0, &mode);
				ScreenWidth = mode.Width;
				ScreenHeight = mode.Height;
		}
#endif
}

DirectXCapture::~DirectXCapture(){

}

bool DirectXCapture::InitDirectX(){
#ifdef D3DX
		m_pD3D = Direct3DCreate9( D3D_SDK_VERSION);
		if(!m_pD3D)
				return false;
		D3DPRESENT_PARAMETERS d3dpp = {0};
		d3dpp.Windowed = TRUE;
		d3dpp.SwapEffect = D3DSWAPEFFECT_DISCARD;
		d3dpp.BackBufferFormat = D3DFMT_UNKNOWN;
		d3dpp.FullScreen_RefreshRateInHz = D3DPRESENT_RATE_DEFAULT;
		d3dpp.PresentationInterval = D3DPRESENT_INTERVAL_IMMEDIATE;
		d3dpp.SwapEffect = D3DSWAPEFFECT_DISCARD;
		d3dpp.EnableAutoDepthStencil = TRUE;
		d3dpp.AutoDepthStencilFormat = D3DFMT_D16;
		HRESULT hrResult = m_pD3D->CreateDevice( D3DADAPTER_DEFAULT, D3DDEVTYPE_HAL, (HWND)parent->winId(),
																						 D3DCREATE_HARDWARE_VERTEXPROCESSING,
																						 &d3dpp, &m_pd3dDevice );
		if(FAILED(hrResult))return false;
#endif
		return true;
}

void DirectXCapture::Capture(){
#ifdef D3DX
		myTimer.start();
		m_pd3dDevice->CreateOffscreenPlainSurface(ScreenWidth, ScreenHeight,
				  D3DFMT_A8R8G8B8, D3DPOOL_SYSTEMMEM, &m_pSurface, NULL);
		m_pd3dDevice->GetFrontBufferData(0, m_pSurface);
#endif
}

void DirectXCapture::Release(){
#ifdef D3DX
		m_pSurface->Release();
#endif
		WorkTime = myTimer.elapsed();
}

int DirectXCapture::GetTime(){
		return WorkTime;
}

void DirectXCapture::MakeScreenshot(int x1, int y1, int x2, int y2){
#ifdef D3DX
		/*HRESULT res = m_pd3dDevice->CreateOffscreenPlainSurface(x2-x1, y2-y1,
				  D3DFMT_A8R8G8B8, D3DPOOL_SCRATCH, &m_SelSurface, NULL);
		if(res == D3DERR_INVALIDCALL) qDebug("Surface create error\n");
		RECT src = {x1,y1,x2,y2};
		RECT dest = {0,0,x2-x1,y2-y1};
		SelRect = dest;
		res = m_pd3dDevice->StretchRect(m_pSurface,&src,m_SelSurface,&dest,D3DTEXF_NONE);
		if(res == D3DERR_INVALIDCALL) qDebug("Stretch error\n");*/

		RECT src = {x1,y1,x2,y2};
		D3DLOCKED_RECT rect;
		HRESULT res = m_pSurface->LockRect(&rect,&src,D3DLOCK_NO_DIRTY_UPDATE | D3DLOCK_NOSYSLOCK | D3DLOCK_READONLY);
		BYTE *bytePointer=(BYTE*)rect.pBits;
		int r = 0, g = 0, b = 0, index = 0;
		for(int x=0;x<x2-x1;x++){
			for(int y=0;y<y2-y1;y++){
				index=(x*4+(y*(rect.Pitch)));
				r = (r+bytePointer[index+2])/2;
				g = (g+bytePointer[index+1])/2;
				b = (b+bytePointer[index])/2;
			}
		}
		color = QColor(r,g,b);
		m_pSurface->UnlockRect();
#endif
}

void DirectXCapture::SaveToFile(char* filename){
#ifdef D3DX
		//D3DXSaveSurfaceToFile(filename,D3DXIFF_JPG,m_SelSurface,NULL,NULL);
#endif
}

QColor DirectXCapture::GetAverage(){
#ifdef D3DX
		/*IDirect3DSurface9* surf;
		m_pd3dDevice->CreateOffscreenPlainSurface(1, 1,
				  D3DFMT_A8R8G8B8, D3DPOOL_SCRATCH, &surf, NULL);
		RECT dest ={0,0,1,1};
		m_pd3dDevice->StretchRect(m_pSurface,&SelRect,surf,&dest,D3DTEXF_NONE);
		surf->Release();*/
		return color;
#endif
		return QColor(0,0,0);
}
